package com.ny.controller;
import com.ny.entity.Result;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ny.entity.Device;

import org.springframework.web.bind.annotation.PathVariable;




@RestController
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    // 查询所有设备信息：http://127.0.0.1:8081/devices
    @GetMapping("/findAll")
    public Result findAll() {
        return Result.success(deviceService.findAll());

    }


    // 根据设备ID查询单个设备信息：http://127.0.0.1:8081/device/1
    @GetMapping("/device/{id}")
    public Result findById(@PathVariable("id") Integer deviceId) {
        try {
            Device device = deviceService.findById(deviceId);
            if (device != null) {
                return Result.success(device);
            } else {
                return Result.error("未找到ID为 " + deviceId + " 的设备");
            }
        } catch (Exception e) {
            return Result.error("查询设备失败: " + e.getMessage());
        }
    }
}